package livesun.justiceeternal.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.text.TextUtils;

/**
 * 类描述：
 * 创建人：livesun
 * 创建时间：2017/9/7
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */

public class PackageUtils {

    /**
     * 获取版本名
     * @return
     */
    public static String getVersionName(){
        Context context = UiUtils.getContext();
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
            String version = packInfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();
            return "getVersionError";

        }
    }

    /**
     * 获取设备ID
     * @return
     */
    public static String getDeviceId() {
        Context context = UiUtils.getContext();
        String androidId = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        if(!TextUtils.isEmpty(androidId)){
            return androidId;

        }else{
            WifiManager wifi = (WifiManager) context.getApplicationContext()
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            return info.getMacAddress();
        }
    }

    /*
 * 获取当前程序的版本号
 */
    private static int getVersionCode() {
        Context context = UiUtils.getContext();
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
            int version = packInfo.versionCode;
            return version;
        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();

        }
        return 0;
    }

    //检查更新

    public static boolean checkUpdate(int updateCode){
        if(getVersionCode()!=0&&getVersionCode()<=updateCode){

            return true;//需要更新
        }

        return false;
    }
}
